package com.sdyd.antivirus.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sdyd.antivirus.utils.DateUtil;
import com.sdyd.antivirus.utils.UUIDUtils;

public class AnswerInfo {
    private String userId;

    private String date;

    private String questionIds;

    private String answers;

    private String groupId;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId == null ? null : userId.trim();
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date == null ? null : date.trim();
    }

    public String getQuestionIds() {
        return questionIds;
    }

    public void setQuestionIds(String questionIds) {
        this.questionIds = questionIds == null ? null : questionIds.trim();
    }

    public String getAnswers() {
        return answers;
    }

    public void setAnswers(String answers) {
        this.answers = answers == null ? null : answers.trim();
    }

    public String getGroupId() {
        return groupId;
    }

    public void setGroupId(String groupId) {
        this.groupId = groupId == null ? null : groupId.trim();
    }

    public static AnswerInfo allocByJson(JSONObject json) {
        AnswerInfo info = new AnswerInfo();
        String groupId = json.getString("groupId");
        JSONArray array = json.getJSONArray("question");
        if (groupId == null || array == null)
            return null;

        info.setGroupId(groupId);
        info.setUserId(UUIDUtils.getUUID());
        info.setDate(DateUtil.getTodayDate());

        // question 字段
        JSONArray jIds = new JSONArray();
        JSONArray jAnswers = new JSONArray();
        for (int i = 0; i < array.size(); i++) {
            JSONObject obj = array.getJSONObject(i);
            jIds.add(obj.getString("id"));
            JSONArray answers = obj.getJSONArray("answers");
            if (answers == null) {
                jAnswers.add(new JSONArray());
            }
            else {
                jAnswers.add(answers);
            }
        }

        info.setQuestionIds(jIds.toString());
        info.setAnswers(jAnswers.toString());

        return info;
    }
}